Skip to content

[CodeCompletion] Add keypath apply subscript after open bracket #31980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented May 22, 2020

func foo(val: String) {
  _ = val[#^HERE^#
}

should suggest keyPath: argument.

Add (keyPath: KeyPath<BaseType, T>) -> T type in the callee analysis for subscript expressions.

rdar://problem/61016147

@rintaro
Copy link
Member Author

rintaro commented May 22, 2020

@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented May 22, 2020

@swift-ci Please smoke test Windows

@rintaro rintaro requested a review from benlangmuir May 22, 2020 22:08

// Add 'subscript<T>(keyPath: KeyPath<Root, T>) -> T'.
if (name.getBaseName() == DeclBaseName::createSubscript() &&
(baseTy->getAnyNominal() || baseTy->is<ArchetypeType>())) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I didn't know this works:

func foo(val: (String, Int)) {
  _ = val[keyPath: \.0]
}

This limitation is not needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for meta type types, the only possible keypath is \.self (e.g. String.self[keyPath: \.self]) because keypath cannot refer static members. @benlangmuir Do you think limiting this to non-metatype types here makes sense?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up with just adding || baseTy->is<TupleType>() here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no strong opinion. Seems fine to me either way.

@rintaro rintaro force-pushed the ide-completion-keypathsubscript-rdar61016147 branch from 5fb4f13 to 38d8b13 Compare June 9, 2020 18:04
@rintaro
Copy link
Member Author

rintaro commented Jun 9, 2020

@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented Jun 9, 2020

@swift-ci Please smoke test Linux

@rintaro rintaro merged commit 617189f into swiftlang:master Jun 10, 2020
@rintaro rintaro deleted the ide-completion-keypathsubscript-rdar61016147 branch June 10, 2020 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants